package cartagows.wsframework.wscoordination;

import java.util.ArrayList;
import java.util.HashMap;

import cartago.Artifact;
import cartago.GUARD;
import cartago.OPERATION;
import cartago.OpFeedbackParam;
import cartagows.WSMsgInfo;

public class ActivitiesMessagesWarehouse extends Artifact{ 

	HashMap<String, ArrayList<WSMsgInfo>> hashMap;
		
	@OPERATION void init(){
		hashMap = new HashMap<String, ArrayList<WSMsgInfo>>();
	}
	
	/**
	 * Operation used for add a new activity in the warehouse
	 * @param activityId
	 */
	@OPERATION(guard="checkActivityAbsence") void addActivity(String activityId){
		ArrayList<WSMsgInfo> a = new  ArrayList<WSMsgInfo>();
		hashMap.put(activityId, a);
	}

	/**
	 * Operation used for add a new message for the activity specified in input in the warehouse
	 * @param activityId
	 * @param msg
	 */
	@OPERATION (guard="checkActivityPresence")void insertMessage(String activityId, WSMsgInfo msg){
		hashMap.get(activityId).add(msg);
	} 
	
	/**
	 * Operation used for retrieve the next message relates to the activityID given in input
	 * @param activityId
	 */
	@OPERATION(guard="checkMsgAvailability") void getNextMessage(String activityId, OpFeedbackParam<WSMsgInfo> res){
		WSMsgInfo msg = hashMap.get(activityId).get(0);
		hashMap.get(activityId).remove(0);
		res.set(msg);
	}
	
	/**
	 * Operation used for delete an activity from the warehouse
	 * @param activityId
	 */
	@OPERATION (guard="checkActivityPresence")void removeActivity(String activityId){
		hashMap.remove(activityId);
	} 
	
	/**
	 * 
	 * @param activityId
	 * @return
	 */
	@GUARD boolean checkMsgAvailability(String activityId, OpFeedbackParam<WSMsgInfo> res){
		if((hashMap.containsKey(activityId)) && (hashMap.get(activityId).size()!=0)) return true;
		else
			return false;
	}
	
	/**
	 * 
	 * @param activityId
	 * @return
	 */
	@GUARD boolean checkActivityAbsence(String activityId){
		return (!hashMap.containsKey(activityId));
	}

	/**
	 * 
	 * @param activityId
	 * @return
	 */
	@GUARD boolean checkActivityPresence(String activityId){
		return hashMap.containsKey(activityId);
	}

	/**
	 * 
	 * @param activityId
	 * @param msg
	 * @return
	 */
	@GUARD boolean checkActivityPresence(String activityId,WSMsgInfo msg){
		return hashMap.containsKey(activityId);
	}
}